package com.example.badmintonUser.user.UserServerImpl;
import com.example.badmintonCommon.entity.Order;
import com.example.badmintonCommon.entity.User;
import com.example.badmintonCommon.util.JwtUtil;
import com.example.badmintonCommon.util.ThreadLocalUtil;
import com.example.badmintonDB.GymDBserver;
import com.example.badmintonDB.UserDBServer;
import com.example.badmintonUser.user.UserServer;
import org.springframework.beans.factory.annotation.Autowired;
import com.example.badmintonCommon.pojo.Result;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServerImpl implements UserServer {

    private final UserDBServer userDBServer;
    private final GymDBserver gymDBserver;
    @Autowired
    public UserServerImpl(UserDBServer userDBServer,GymDBserver gymDBserver){
      this.userDBServer=userDBServer;
      this.gymDBserver=gymDBserver;
    }

    //用户通过手机号登录
    @Override
    public Result<String> UserLogin(String tel, String psw) {
        System.out.println(userDBServer.existsByUserTel(tel));
        if (!userDBServer.existsByUserTel(tel)) {

            return Result.error("不存在该用户");
        }

        // 从 DB 取出已加密的密码（BCrypt 格式）
        String encodedPsw = userDBServer.getPasswordByTel(tel);
        int islock = userDBServer.getIslockByTel(tel);
        if (islock ==1) {
            return Result.error("用户为黑名单");
        }

        if (encodedPsw == null) {
            return Result.error("用户密码不存在");
        }

        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        if (!encoder.matches(psw, encodedPsw)) {
            return Result.error("密码错误");
        }

        // 验证通过，生成 token 返回
        Map<String, Object> claims = new HashMap<>();
        claims.put("tel", tel);
        String token = JwtUtil.genToken(claims);
        return Result.success(token);
    }

    //用户通过手机号注册
    @Override
    public Result<String> UserRegister(User user) {

        if (userDBServer.existsByUserTel(user.getTel())) {
              return Result.error("该用户已经存在");
        }
        else{
            user.setRole("普通用户");
            user.setIslock("0");
            // ✅ 使用 BCrypt 对密码加密
            BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
            String encodedPassword = encoder.encode(user.getPsw());
            user.setPsw(encodedPassword);
            userDBServer.RegisterByUserTel(user);
            System.out.println(user);
            return Result.success();
            //返回200则说明注册成功
            //return Result.success("注册成功");
        }
    }

    //通过手机号查看用户所有订单
    @Override
    public Result<List<Order>> ShowAllOderByTel(String tel){
        if (userDBServer.existsByUserTel(tel)){
            return Result.success(userDBServer.ShowAllOderByTel(tel));
        }
        else return Result.error();
    }

    //获取用户信息
    @Override
    public Result<User> getUserInfo() {
        Map<String,String> map= ThreadLocalUtil.get();
        String tel=map.get("tel");
        User user;
        try{
            user=userDBServer.getUserInfo(tel);
        }catch (Exception E){
            return Result.error();
        }
        return Result.success(user);
    }

    //用户修改信息
    @Override
    public Result<String> editmessage(String tel,String name,String sex){
        userDBServer.editmessage(tel,name,sex);
        return Result.success("修改成功");
    }

    //用户修改密码
    @Override
    public Result<String> editpsw(String tel,String oldpsw,String newpsw) {

        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String psw = userDBServer.getPasswordByTel(tel);
        if (encoder.matches(oldpsw, psw)) {
            String encodedPassword = encoder.encode(newpsw);
            userDBServer.editpsw(tel, encodedPassword);
            return Result.success("修改成功");
        }else{
            return Result.success("密码错误");
        }
    }

}
